<template>
  <el-container>
    <!-- 侧边栏 :width="isCollapse ? '0px':'200px'"-->
    <el-aside :width="isCollapse ? '0px':'200px'">
      <div class="logo">商品后台管理</div>
      <el-menu
        class="el-menu-vertical-demo"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#0080ff"
        router
        :default-active="routerActive"
        unique-opened>        
        <el-submenu :index="''+item.id" v-for="(item, index) in menuList" :key="index">
          <template slot="title">
            <i :class="icons[index]"></i>
            <span>{{item.authName}}</span>
          </template>
          <el-menu-item :index="'/'+itemChild.path"
            v-for="itemChild in item.children" :key="itemChild.id"
            @click="saveRoutState(itemChild.path)">
            <i class="el-icon-menu"></i>
            <span slot="title">{{itemChild.authName}}</span>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <!-- 页面主体 -->
    <el-container>
      <!-- 页面头部 -->
      <el-header>
        <!-- 侧边折叠按钮 -->
        <div class="cp">
          <i class="el-icon-s-fold" :class="{'active': isCollapse}" @click="collapse"></i>
        </div>
        <!-- 右侧用户设置 -->
        <el-dropdown @command="handleCommand" trigger="click">
          <span class="el-dropdown-link">
            <i>{{username}}</i>
            <i class="el-icon-arrow-down el-icon--right"></i>
            <i class="el-icon-success" style="color: lightgreen;"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="info">
              <el-badge is-dot>基本资料</el-badge>
            </el-dropdown-item>
            <el-dropdown-item command="safe">
              <el-badge is-dot>安全设置</el-badge>
            </el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <!-- 页面内容部分 -->
      <el-main>
        <!-- 子路由 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { menus } from '@/api/data/menus.js'
export default {
  data() {
    return {
      username: '',
      // 左侧菜单是否折叠
      isCollapse: false,
      // 左侧菜单列表
      menuList: [],
      icons: ['el-icon-s-custom','el-icon-coin','el-icon-goods','el-icon-s-order','el-icon-s-data'],
      routerActive: ''
    }
  },
  created() {
    this.getUserInfo()
    this.getMenuList()
    this.getRoutState()
  },
  methods: {
    // 获用户名
    getUserInfo() {
      const res = window.sessionStorage.getItem('user')
      this.username = res
    },
    // 退出系统
    logout(){
      // 清空token
      window.sessionStorage.clear()
      // 跳转至登录页
      this.$router.push("/login")
    },
    // 是否折叠左侧菜单
    collapse(){
      this.isCollapse = !this.isCollapse
    },
    // 处理头部菜单点击事件
    handleCommand(command) {
      switch (command) {
        case "info":
          this.$message('click on item ' + command);
          break;
        case "safe":
          this.$message('click on item ' + command);
          break;
        case "logout":
          this.$confirm('是否退出系统?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.logout()
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消退出'
            });          
          });
          break;
      }
    },
    // 获取菜单列表
    async getMenuList() {
      // this.menuList = menus
      const {data: res} = await this.$axios.get('/menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // this.$message({message: res.meta.msg, type: 'success'})
      this.menuList = res.data
      //console.log(res);
    },
    // 保存菜单激活状态
    saveRoutState(routerActive) {
      window.sessionStorage.setItem('routerActive', '/'+routerActive)
    },
    // 获取菜单激活状态
    getRoutState() {
      this.routerActive = window.sessionStorage.getItem('routerActive')
    }
  },
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-aside {
  background: #545c64;
  transition: all .4s linear;
  color: #fff;
  overflow: hidden;
  .logo {
    width: 200px;
    height: 60px;
    text-align: center;
    line-height: 60px;
  }
  .el-menu {
    border: none;
  }
}
.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #f8f6f8;
  .cp {
    i {
      font-size: 26px;
      transition: all .3s linear;
    }
    i.active {
      /* transition: all .3s linear; */
      transform: rotateZ(180deg);
    }
  }
  .el-dropdown {
    display: flex;
    align-items: center;
  }
}
</style>